package menu

import (
	"context"
	// "fmt"
	"strconv"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/system/util"
	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/rpc/backendclient"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetMenuListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetMenuListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuListLogic {
	return &GetMenuListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetMenuListLogic) GetMenuList(req *types.SystemMenuListReq) (resp *types.SystemMenuPlainInfoListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorter(req.Sorter)
	filterBys := util.ParseFilter(req.Filter)

	var ids *string
	if req.Id != nil && *req.Id > 0 {
		ids = pointy.GetPointer(strconv.FormatInt(int64(*req.Id), 10))
	}

	data, err := l.svcCtx.BackendRpc.GetMenuList(l.ctx, &backendclient.MenuListReq{
		Page:      page,
		PageSize:  pageSize,
		Ids:       ids,
		ParentId:  req.ParentId,
		Type:      req.Type,
		Level:     req.Level,
		Path:      req.Path,
		Name:      req.Name,
		Status:    req.Status,
		Roles:     req.Roles,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		OrderBy:   orderBys,
		FilterBy:  filterBys,
	})
	if err != nil {
		return
	}

	resp = &types.SystemMenuPlainInfoListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.SystemMenuPlainInfoList{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
		},
	}
	for _, v := range data.List {
		option := utils.GetOptionByStatus(v.Status)
		resp.Data.List = append(resp.Data.List, &types.SystemMenuPlainInfo{
			Id:                 v.Id,
			Trans:              l.svcCtx.Trans.Trans(l.ctx, *v.Meta.Title),
			ParentId:           v.ParentId,
			Type:               v.Type,
			Level:              v.Level,
			Path:               v.Path,
			Name:               v.Name,
			Redirect:           v.Redirect,
			Component:          v.Component,
			Sort:               v.Sort,
			Status:             v.Status,
			Title:              v.Meta.Title,
			Icon:               v.Meta.Icon,
			HideMenu:           v.Meta.HideMenu,
			HideBreadcrumb:     v.Meta.HideBreadcrumb,
			IgnoreKeepAlive:    v.Meta.IgnoreKeepAlive,
			HideTab:            v.Meta.HideTab,
			FrameSrc:           v.Meta.FrameSrc,
			CarryParam:         v.Meta.CarryParam,
			HideChildrenInMenu: v.Meta.HideChildrenInMenu,
			Affix:              v.Meta.Affix,
			DynamicLevel:       v.Meta.DynamicLevel,
			RealPath:           v.Meta.RealPath,
			CreatedAt:          v.CreatedAt,
			UpdatedAt:          v.UpdatedAt,
			DeletedAt:          v.DeletedAt,
			CanAccept:          option.CanAccept,
			CanReject:          option.CanReject,
			CanRemove:          option.CanRemove,
			CanRestore:         option.CanRestore,
		})
	}

	return resp, nil
}
